<template>
  <div class="register-container">
    <span class="form-title">后台管理系统</span>
    <div class="form-container">
      <el-form ref="form" :model="form" :rules="rules" label-suffix="：" label-width="100px">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input v-model="form.checkPassword" placeholder="请确认密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="选择用户">
          <el-select v-model="form.role" placeholder="请选择">
            <el-option
              v-for="(item,index) in roleList"
              :key="index"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="submit-btn" type="primary" @click="onSubmit">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="tips">
        <span>已有账号？返回<router-link to="/">登录</router-link></span>
      </div>
    </div>
  </div>
</template>

<script>

import { userRegister } from '@/api/user'

export default {
  name: 'RegisterView',
  data () {
    const validatePassWorld = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        password: '',
        checkPassword: '',
        role: ''
      },
      roleList: [],
      rules: {
        name: [{
          required: true,
          message: '用户名不能为空',
          trigger: 'blur'
        }, {
          min: 2,
          max: 30,
          message: '用户名长度在2-30个字符之间',
          trigger: 'blur'
        }],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 30,
            message: '密码长度要求6-30个字符之间',
            trigger: 'blur'
          }
        ],
        checkPassword: [{
          required: true,
          message: '确认密码不能为空',
          trigger: 'blur'
        }, {
          min: 6,
          max: 30,
          message: '密码长度在6-30个字符之间',
          trigger: 'blur'
        }, {
          // 使用自定义校验
          validator: validatePassWorld,
          trigger: 'blur'
        }]
      }
    }
  },
  mounted () {
    // 挂载初始化用户列表
    this.getRoleList()
  },
  methods: {
    getRoleList () {
      this.roleList = [
        {
          label: '管理员',
          value: 'admin'
        },
        {
          label: '普通用户',
          value: 'user'
        }
      ]
    },
    onSubmit () {
      this.$refs.form.validate((valid) => {
        // 判断校验失败的情况
        if (!valid) {
          this.$notify.error({
            title: '错误',
            message: '校验不通过'
          })
          return
        }
        const data = {
          account: this.form.name,
          password: this.form.password,
          userGroup: this.form.role
        }
        userRegister(data)
          .then(res => {
            const {
              code,
              msg
            } = res
            if (code !== 0) {
              return this.$notify.error({
                title: '错误',
                message: msg
              })
            }
            this.$notify.success({
              title: '成功',
              message: msg
            })
            // 跳转到新的页面
            this.$router.push('/login')
          })
          .catch((err) => {
            this.$message.error(err)
            console.log(err)
          })
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  position: relative;
  height: 100%;
  width: 100%;
  background: url("@/assets/bg.jpg") no-repeat center center;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.form-title {
  margin: 15px;
  font-size: 24px;
  color: #333;
  font-weight: 700;
}

.submit-btn {
  width: 100%;
}

.form-container {
  background: #ffff;
  border-radius: 10px;
  padding: 50px;
}

.tips {
  font-size: 12px;
  text-align: right;
  color: #333;
}

.tips span a {
  margin: 0 2px;
  color: #409eff;
}
</style>
